<template>
  <el-dialog
    width="40%"
    :title="!dataForm.id ? $t('Common.Add') : $t('Common.Edit')"
    :close-on-click-modal="false"
    :visible.sync="visible"
    class="hotel-protocol"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="120px"
    >
      <el-form-item label="是否自动取消" prop="code">
        <el-switch
          v-model="dataForm.isAutoCancel"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="1"
          :inactive-value="0"
          active-text="是"
          inactive-text="否"
        ></el-switch>
      </el-form-item>
      <el-form-item label="自动取消间隔" prop="name">
        <el-input style="width: 100px"  v-model="dataForm.autoCancelTime" placeholder="自动取消间隔"></el-input> 分钟
      </el-form-item>
    <!--  <el-form-item label="是否自动入住" prop="name">
        <el-switch
          v-model="dataForm.isAutoCheckIn"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="1"
          :inactive-value="0"
          active-text="是"
          inactive-text="否"
        ></el-switch>
      </el-form-item>-->
      <el-form-item label="是否自动好评" prop="name">
        <el-switch
          v-model="dataForm.isAutoPraise"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="1"
          :inactive-value="0"
          active-text="是"
          inactive-text="否"
        ></el-switch>
      </el-form-item>
      <el-form-item label="自动好评间隔" prop="name">
        <el-input style="width: 100px" v-model="dataForm.autoCheckInTime" placeholder="自动好评间隔"></el-input> 分钟
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{ $t('Common.Cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmit()">{{ $t('Common.Confirm') }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: undefined,
        hotelId: -1,
        isAutoCancel: 0,
        autoCancelTime: 0,
        isAutoCheckIn: 0,
        isAutoPraise: 0,
        autoCheckInTime: 0
      },
      dataRule: {}
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id != undefined) {
          this.$http({
            url: this.$http.adornUrl(
              `/hotel/hotelBusinessSetting/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.id = data.hotelBusinessSetting.id;
              this.dataForm.hotelId = data.hotelBusinessSetting.hotelId;
              this.dataForm.isAutoCancel =
                data.hotelBusinessSetting.isAutoCancel;
              this.dataForm.autoCancelTime =
                data.hotelBusinessSetting.autoCancelTime;
              this.dataForm.isAutoCheckIn =
                data.hotelBusinessSetting.isAutoCheckIn;
              this.dataForm.isAutoPraise =
                data.hotelBusinessSetting.isAutoPraise;
              this.dataForm.autoCheckInTime =
                data.hotelBusinessSetting.autoCheckInTime;
            }
          });
        } else {
          this.dataForm = {
            id: undefined,
            hotelId: -1,
            isAutoCancel: 0,
            autoCancelTime: 0,
            isAutoCheckIn: 0,
            isAutoPraise: 0,
            autoCheckInTime: 0
          };
        }
      });
    },
    // code纯数字校验
    codeRE(rule, value, callback) {
      const reg = /^[0-9]*$/;
      if (!value) {
        return callback(new Error("问题编码不能为空"));
      } else if (!reg.test(value)) {
        return callback(new Error("问题编码必须是数字"));
      } else {
        callback();
      }
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/hotel/hotelBusinessSetting/${
                this.dataForm.id !== undefined ? "update" : "save"
              }`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id,
              hotelId: this.dataForm.hotelId,
              isAutoCancel: this.dataForm.isAutoCancel,
              autoCancelTime: this.dataForm.autoCancelTime,
              isAutoCheckIn: 0,
              isAutoPraise: this.dataForm.isAutoPraise,
              autoCheckInTime: this.dataForm.autoCheckInTime
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    onEditorBlur() {},
    onEditorFocus() {
      //获得焦点事件
    },
    onEditorChange() {
      //内容改变事件
    }
  }
};
</script>
<style lang="less">
// .hotel-protocol {
//   .el-dialog__body {
//     height: 80vh;
//   }
// }
</style>

